package com.cq.hd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cq.hd.admin.mapper.TbAdminPermissionMapper;
import com.cq.hd.admin.po.TbAdminPermissionPo;
import com.cq.hd.admin.service.TbAdminPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cq.hd.common.enums.biz.DeleteStatusEnum;
import com.cq.hd.common.utils.LocalDateUtil;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2021-10-28
 */
@Service
public class TbAdminPermissionServiceImpl extends ServiceImpl<TbAdminPermissionMapper, TbAdminPermissionPo> implements TbAdminPermissionService {

    @Override
    public Set<String> getSetByRoleIds(Integer roleId) {
        Set<String> permissions = new HashSet<String>();
        if (roleId == null) {
            return permissions;
        }
        List<TbAdminPermissionPo> permissionList = baseMapper.selectList(new QueryWrapper<TbAdminPermissionPo>().in("role_id", roleId).eq("delete_status", false));
        for (TbAdminPermissionPo permission : permissionList) {
            permissions.add(permission.getPermission());
        }
        return permissions;
    }

    @Override
    public void addPermission(TbAdminPermissionPo adminPermission) {
        adminPermission.setCreateTime(LocalDateUtil.getNowLocalDateTime());
        adminPermission.setUpdateTime(LocalDateUtil.getNowLocalDateTime());
        this.baseMapper.insert(adminPermission);
    }

    @Override
    public void deleteByRoleId(Integer roleId) {
        this.baseMapper.update(null, Wrappers.<TbAdminPermissionPo>lambdaUpdate()
                .eq(TbAdminPermissionPo::getRoleId, roleId)
                .set(TbAdminPermissionPo::getDeleteStatus, DeleteStatusEnum.DELETED.getValue())
                .set(TbAdminPermissionPo::getUpdateTime, LocalDateUtil.getNowLocalDateTime())
        );
    }

    @Override
    public Set<String> getSetPermissionByRoleId(Integer roleId) {
        Set<String> permissions = new HashSet<String>();
        if (roleId == null) {
            return permissions;
        }
        List<TbAdminPermissionPo> permissionList = this.baseMapper.selectList(new QueryWrapper<TbAdminPermissionPo>()
                .eq("role_id", roleId)
                .eq("delete_status", DeleteStatusEnum.NORMAL.getValue())
        );
        for (TbAdminPermissionPo permission : permissionList) {
            permissions.add(permission.getPermission());
        }
        return permissions;
    }

    @Override
    public boolean checkSuperPermission(Integer roleId) {
        if (roleId == null) {
            return false;
        }
        Integer permissionEntity = this.baseMapper.selectCount(new QueryWrapper<TbAdminPermissionPo>()
                .eq("permission", "*")
                .eq("role_id", roleId)
                .eq("delete_status", DeleteStatusEnum.NORMAL.getValue())
        );
        return permissionEntity != 0;
    }
}
